﻿using System.ComponentModel.DataAnnotations;
using User.EF.Model;
using User.Global.Constvalues;
using User.Global.Contracts.Membership;
using User.Global.Resources;
using User.Web.Common.WebAttributes;
using User.Web.Contracts.Model.Account;

namespace User.Web.Models.Account
{
    /// <summary>
    /// Das Aktuelle Model zum Registrieren eines Users.
    /// Achtung leitet von ModelBase ab.
    /// </summary>
    public class RegisterModel : ModelBase, IRegisterModel
    {
        #region Member
        [Required]
        [DataType(DataType.Password)]
        [StringLength(100, ErrorMessageResourceType = typeof(ErrorMessages), ErrorMessageResourceName = "MinPasswordLength", MinimumLength = ConstLengthValues.MinPasswordLength)]
        [Display(ResourceType = typeof(InputFieldNames), Name = "Password")]
        [HtmlAttributes(CssClass = "form-control", Required = true, Placeholder = "Passwort")]
        public string Password { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(ResourceType = typeof(InputFieldNames), Name = "PasswordConfirm")]
        [Compare("Password", ErrorMessageResourceType = typeof(ErrorMessages), ErrorMessageResourceName = "PasswordsDoNotMatch")]
        [HtmlAttributes(CssClass = "form-control", Required = true, Placeholder = "Passwort bestätigen")]
        public string ConfirmPassword { get; set; }

        [Required]
        [EmailAddress]
        [DataType(DataType.EmailAddress)]
        [Display(ResourceType = typeof(InputFieldNames), Name = "Username")]
        [HtmlAttributes(CssClass = "form-control", Required = true, Placeholder = "Email (Username)")]
        public string Username { get; set; }
        #endregion

        #region Public Functions
        /// <summary>
        /// Erstellen des Users der in den Modeldaten angebeben wurde.
        /// </summary>
        /// <returns>TRUE->Erfolgreich angelegt | FALSE->Fehler beim Anlegen</returns>
        public virtual bool CreateUser(IDiMembership<Member> membership)
        {
            // Attempt to register the user
            bool creation = membership.CreateUser(Username, Password, new Member());
            return creation;
        }
        #endregion
    }
}
